package com.hinnotek.amoeba.ui.activity.old

import android.os.Bundle
import android.view.View
import androidx.activity.viewModels
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.lee.common.base.BaseActivity
import com.hinnotek.amoeba.R
import com.hinnotek.amoeba.adapters.old.TextViewAdapter
import com.hinnotek.amoeba.base.BaseBean
import com.hinnotek.amoeba.bean.old.AmibaInstallByIdBean
import com.hinnotek.amoeba.databinding.ActivityTiOtherAmoebaInstallingDetailBinding
import com.hinnotek.amoeba.dialog.CustomDialog
import com.hinnotek.amoeba.utilities.ToastUtils
import com.hinnotek.amoeba.viewmodels.AmibaInstallViewModel
import com.lee.common.network.net.ObserverState
import com.xuexiang.xutil.common.StringUtils
import dagger.hilt.android.AndroidEntryPoint
import java.util.*

@AndroidEntryPoint
class TIOtherinsViewingDetailActivity :
    BaseActivity<ActivityTiOtherAmoebaInstallingDetailBinding>() {
    private var adapter: TextViewAdapter? = null
    private var amibaInstallId = 0

    val viewModel by viewModels<AmibaInstallViewModel>()


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setLeftVisible(true)
        setTitle("其他阿米巴安装申请")

        setContentView(R.layout.activity_ti_other_amoeba_installing_detail)
    }

    override fun initVew() {
        val manager = LinearLayoutManager(mContext)
        manager.orientation = RecyclerView.VERTICAL

        bindingView.insView.rlvTiContinuedPriceIncrease.layoutManager = manager
        bindingView.insView.rlvTiContinuedPriceIncrease.adapter =
            TextViewAdapter(R.layout.item_text_view).also {
                adapter = it
            }

        bindingView.superButtonInstalled.setOnClickListener {
            updateAmibaInstallCompleteState(
                amibaInstallId
            )
        }
        bindingView.superButtonSubmit.setOnClickListener(View.OnClickListener { v: View? ->
            val continuedMoney =
                bindingView.edTiContinuedPriceIncrease.text.toString().trim()
            if (StringUtils.isEmpty(continuedMoney)) {
                ToastUtils.show(mContext, "请填写续增价")
                return@OnClickListener
            }
            addAmibaInstallContinued(amibaInstallId, continuedMoney)
        })
    }

    override fun setData() {
        amibaInstallId = intent.getIntExtra("amibaInstallId", 0)
        getAmibaInstallById(amibaInstallId)
    }

    /**
     * app端更新安装完成状态接口
     */
    private fun updateAmibaInstallCompleteState(amibaInstallId: Int) {
        val customDialog = CustomDialog(mContext, "正在加载...")
        customDialog.show()

        viewModel.updateAmibaInstallCompleteState(amibaInstallId)
            .observe(this, object : ObserverState<BaseBean>() {
                override fun onDataChange(data: BaseBean) {
                    customDialog.dismiss()
                    finishActivity()
                    super.onDataChange(data)
                }

                override fun onError(msg: String?, code: Int) {
                    customDialog.dismiss()
                    ToastUtils.showShort(mContext, msg)
                    super.onError(msg, code)
                }

            })
    }

    /**
     * app端添加续增价接口
     */
    fun addAmibaInstallContinued(amibaInstallId: Int, continuedMoney: String) {
        val customDialog = CustomDialog(mContext, "正在加载...")
        customDialog.show()

        viewModel.addAmibaInstallContinued(amibaInstallId, continuedMoney)
            .observe(this, object : ObserverState<BaseBean>() {

                override fun onDataChange(data: BaseBean) {
                    customDialog.dismiss()
                    finishActivity()
                    super.onDataChange(data)
                }

                override fun onError(msg: String?, code: Int) {
                    customDialog.dismiss()
                    ToastUtils.showShort(mContext, msg)
                    super.onError(msg, code)
                }

            })
    }

    /**
     * app端通过id获取单个安装申请接口
     */
    fun getAmibaInstallById(amibaInstallId: Int) {
        val customDialog = CustomDialog(mContext, "正在加载...")
        customDialog.show()

        viewModel.getAmibaInstallById(amibaInstallId).observe(this, object :
            ObserverState<AmibaInstallByIdBean>() {
            override fun onDataChange(data: AmibaInstallByIdBean) {

                bindingView.insView.tvTiChooseAmoeba.text = data.orgName
                bindingView.insView.tvTiUnit.text = data.customerName
                bindingView.insView.tvTiRelatedProject.text = data.projectName
                bindingView.insView.tvTiInstallDate.text = data.installDate
                bindingView.insView.tvTiEndDate.text = data.endDate
                bindingView.insView.tvTiInstallLocation.text = data.installPosition
                bindingView.insView.tvTiPreQuote.text = data.preMoney
                bindingView.insView.tvTiApplicant.text = data.createUser
                bindingView.insView.tvTiApprover.text = data.approvalUser

                val statusView = bindingView.insView.tvTiApplicantStatus

                if (data.approvalState == 1) {
                    statusView.text = "已申请"
                    statusView.setTextColor(getColor21(R.color.black))
                } else if (data.approvalState == 2) {
                    statusView.text = "已同意"
                    statusView.setTextColor(getColor21(R.color.xui_btn_green_normal_color))
                } else if (data.approvalState == 3) {
                    statusView.text = "已拒绝"
                    statusView.setTextColor(getColor21(R.color.xui_config_color_red))
                }
                bindingView.insView.tvTiCc.text = data.sendUser
                val strings: MutableList<String> = ArrayList()
                for (amibaInstallContinueds in data.amibaInstallContinueds) {
                    strings.add(amibaInstallContinueds.continuedMoney)
                }
                adapter?.setList(strings)

                super.onDataChange(data)
            }

            override fun onError(msg: String?, code: Int) {
                customDialog.dismiss()
                ToastUtils.showShort(mContext, msg)
                super.onError(msg, code)
            }
        })

    }
}